<?php
namespace BP\Sys;
require_once("BP/En/Map.php");
class SFDBSrc extends EntityNoName {
	public function getIcon () 
	{
		switch ($this->getDBSrcType()) {
			case $Localhost:
				return "<img src='/WF/Img/DB.gif' />";
			default:
				return "";
		}
	}
	public function getUserID () 
	{
		return $this->GetValStringByKey(SFDBSrcAttr::$UserID);
	}
	public function setUserID ($value) // [String value]
	{
		$this->SetValByKey(SFDBSrcAttr::$UserID, $value);
	}
	public function getPassword () 
	{
		return $this->GetValStringByKey(SFDBSrcAttr::$Password);
	}
	public function setPassword ($value) // [String value]
	{
		$this->SetValByKey(SFDBSrcAttr::$Password, $value);
	}
	public function getDBName () 
	{
		return $this->GetValStringByKey(SFDBSrcAttr::$DBName);
	}
	public function setDBName ($value) // [String value]
	{
		$this->SetValByKey(SFDBSrcAttr::$DBName, $value);
	}
	public function getDBSrcType () 
	{
		return DBSrcType->forValue($this->GetValIntByKey(SFDBSrcAttr::$DBSrcType));
	}
	public function setDBSrcType ($value) // [DBSrcType value]
	{
		$this->SetValByKey(SFDBSrcAttr::$DBSrcType, $value->getValue());
	}
	public function getIP () 
	{
		return $this->GetValStringByKey(SFDBSrcAttr::$IP);
	}
	public function setIP ($value) // [String value]
	{
		$this->SetValByKey(SFDBSrcAttr::$IP, $value);
	}
	public function getHisDBType () 
	{
		switch ($this->getDBSrcType()) {
			case $Localhost:
				return SystemConfig::getAppCenterDBType();
			case $SQLServer:
				return $DBType->MSSQL;
			case $Oracle:
				return $DBType->Oracle;
			case $MySQL:
				return $DBType->MySQL;
			case $Informix:
				return $DBType->Informix;
			default:
				throw new \RuntimeException("err@HisDBType没有判断的数据库类型.");
		}
	}
	public function RunSQLReturnInt ($sql, $isNullAsVal) // [String sql, int isNullAsVal]
	{
		$dt = $this->RunSQLReturnTable($sql);
		if (($dt->Rows->size() == 0))
		{
			return $isNullAsVal;
		}
		return $Integer->parseInt($dt->Rows->get(0)->getValue(0));
	}
	public function RunSQLReturnTable ($runObj) // [String runObj]
	{
		return DBAccess->RunSQLReturnTable($runObj, new Paras());
	}
	public function GetLinkedServerObjName ($objName) // [String objName]
	{
		switch ($this->getDBSrcType()) {
			case $Localhost:
				if ((DBAccess->getAppCenterDBType() != $DBType->MSSQL))
				{
					throw new \RuntimeException("目前只支持CCFlow主数据库为SqlServer的模式，其他数据库类型暂不支持建立数据源�?");
				}
				return $objName;
			case $SQLServer:
				return $String->format("%1$s.%2$s.dbo.%3$s", $this->getNo(), $this->getDBName(), $objName);
			case $Oracle:
				return $String->format("%1$s..%2$s.%3$s", $this->getNo(), $this->getUserID()->toUpperCase(), $objName->toUpperCase());
			case $MySQL:
				return $String->format("OPENQUERY(%1$s,'SELECT * FROM %2$s')", $this->getNo(), $objName);
			case $Informix:
				return $String->format("OPENQUERY(%1$s,'SELECT * FROM %2$s')", $this->getNo(), $objName);
			default:
				throw new \RuntimeException("@未涉及的数据库类型�??");
		}
	}
	public function IsExistsObj ($objName) // [String objName]
	{
		$sql = "";
		$dt = NULL;
		switch ($this->getDBSrcType()) {
			case $Localhost:
				$sql = $this->GetIsExitsSQL(DBAccess->getAppCenterDBType(), $objName, DBAccess->getAppCenterDBType());
				$dt = DBAccess->RunSQLReturnTable($sql);
				break;
			case $SQLServer:
				$sql = $this->GetIsExitsSQL($DBType->MSSQL, $objName, $this->getDBName());
				$dt = $this->RunSQLReturnTable($sql);
				break;
			case $Oracle:
				$sql = $this->GetIsExitsSQL($DBType->Oracle, $objName, $this->getDBName());
				$dt = $this->RunSQLReturnTable($sql);
				break;
			case $MySQL:
				$sql = $this->GetIsExitsSQL($DBType->MySQL, $objName, $this->getDBName());
				$dt = $this->RunSQLReturnTable($sql);
				break;
			case $Informix:
				$sql = $this->GetIsExitsSQL($DBType->Informix, $objName, $this->getDBName());
				$dt = $this->RunSQLReturnTable($sql);
				break;
			default:
				throw new \RuntimeException("@未涉及的数据库类型�??");
		}
		return ( (($dt->Rows->size() == 0)) ? NULL : $dt->Rows->get(0)->getValue(0) );
	}
	public function GetIsExitsSQL ($dbType, $objName, $dbName) // [DBType dbType, String objName, String dbName]
	{
		switch ($dbType) {
			case $MSSQL:
			case $PostgreSQL:
				return $String->format("SELECT (CASE s.xtype WHEN 'U' THEN 'TABLE' WHEN 'V' THEN 'VIEW' WHEN 'P' THEN 'PROCEDURE' ELSE 'OTHER' END) OTYPE FROM sysobjects s WHERE s.name = '%1$s'", $objName);
			case $Oracle:
				return $String->format("SELECT uo.OBJECT_TYPE OTYPE FROM user_objects uo WHERE uo.OBJECT_NAME = '%1$s'", $objName->toUpperCase());
			case $MySQL:
				return $String->format("SELECT (CASE t.TABLE_TYPE WHEN 'BASE TABLE' THEN 'TABLE' ELSE 'VIEW' END) OTYPE FROM information_schema.tables t WHERE t.TABLE_SCHEMA = '%2$s' AND t.TABLE_NAME = '%1$s'", $objName, $dbName);
			case $Informix:
				return $String->format("SELECT (CASE s.tabtype WHEN 'T' THEN 'TABLE' WHEN 'V' THEN 'VIEW' ELSE 'OTHER' END) OTYPE FROM systables s WHERE s.tabname = '%1$s'", $objName);
			case $DB2:
				return $String->format("");
			case $Access:
				return $String->format("");
			default:
				throw new \RuntimeException("@没有涉及的数据库类型�?");
		}
	}
	public static function constructor__ () 
	{
		$me = new self();
		return $me;
	}
	public static function constructor__String ($mypk) // [String mypk]
	{
		$me = new self();
		$me->setNo($mypk);
		$me->Retrieve();
		return $me;
	}
	public function getEnMap () 
	{
		if (($this->get_enMap() != NULL))
		{
			return $this->get_enMap();
		}
		$map = new Map("Sys_SFDBSrc", "数据�?");
		$map->Java_SetDepositaryOfEntity($Depositary->None);
		$map->Java_SetDepositaryOfMap($Depositary->Application);
		$map->Java_SetEnType($EnType->Sys);
		$map->AddTBStringPK(SFDBSrcAttr::$No, NULL, "数据源编�?(必须是英�?)",  TRUE ,  FALSE , 1, 20, 20);
		$map->AddTBString(SFDBSrcAttr::$Name, NULL, "数据源名�?",  TRUE ,  FALSE , 0, 30, 20);
		$map->AddDDLSysEnum(SFDBSrcAttr::$DBSrcType, 0, "数据源类�?",  TRUE ,  TRUE , SFDBSrcAttr::$DBSrcType, "@0=应用系统主数据库(默认)@1=SQLServer数据库@2=Oracle数据库@3=MySQL数据库@4=Informix数据库@50=Dubbo服务@100=WebService数据�?");
		$map->AddTBString(SFDBSrcAttr::$UserID, NULL, "数据库登录用户ID",  TRUE ,  FALSE , 0, 30, 20);
		$map->AddTBString(SFDBSrcAttr::$Password, NULL, "数据库登录用户密�?",  TRUE ,  FALSE , 0, 30, 20);
		$map->AddTBString(SFDBSrcAttr::$IP, NULL, "IP地址/数据库实例名",  TRUE ,  FALSE , 0, 500, 20);
		$map->AddTBString(SFDBSrcAttr::$DBName, NULL, "数据库名�?/Oracle保持为空",  TRUE ,  FALSE , 0, 30, 20);
		$rm = new RefMethod();
		$rm = new RefMethod();
		$rm->Title = "测试连接";
		$rm->ClassMethodName = ($this . ".DoConn");
		$rm->refMethodType = $RefMethodType->Func;
		$map->AddRefMethod($rm);
		$this->set_enMap($map);
		return $this->get_enMap();
	}
	public function getConnString () 
	{
		switch ($this->getDBSrcType()) {
			case $Localhost:
				return SystemConfig::getAppCenterDSN();
			case $SQLServer:
				return (((((((("password=" . $this->getPassword()) . ";persist security info=true;user id=") . $this->getUserID()) . ";initial catalog=") . $this->getDBName()) . ";data source=") . $this->getIP()) . ";timeout=999;multipleactiveresultsets=true");
			case $Oracle:
				return (((((("user id=" . $this->getUserID()) . ";data source=") . $this->getIP()) . ";password=") . $this->getPassword()) . ";Max Pool Size=200");
			case $MySQL:
				return (((((((("Data Source=" . $this->getIP()) . ";Persist Security info=True;Initial Catalog=") . $this->getDBName()) . ";User ID=") . $this->getUserID()) . ";Password=") . $this->getPassword()) . ";");
			case $Informix:
				return (((((((("Host=" . $this->getIP()) . "; Service=; Server=; Database=") . $this->getDBName()) . "; User id=") . $this->getUserID()) . "; Password=") . $this->getPassword()) . "; ");
			case $PostgreSQL:
				return (((((((("Server=" . $this->getIP()) . ";Port=5432;Database=") . $this->getDBName()) . ";UserId=") . $this->getUserID()) . ";Password=") . $this->getPassword()) . ";;Pooling=False;");
			default:
				throw new \RuntimeException("@没有判断的类�?.");
		}
	}
	public function DoConn () 
	{
		if ($this->getNo()->equals("local"))
		{
			return "本地连接不需要测试是否连接成�?.";
		}
		if (($this->getDBSrcType() == DBSrcType::$Localhost))
		{
			return "@在该系统中只能有�?个本地连�?.";
		}
		$dsn = "";
		if (($this->getDBSrcType() == DBSrcType::$SQLServer))
		{
			try 
			{
				try 
				{
					DBAccess::RunSQL((("Exec sp_droplinkedsrvlogin " . $this->getNo()) . ",Null "));
					DBAccess::RunSQL(("Exec sp_dropserver " . $this->getNo()));
				}
				catch (\Exception $e)
				{ /* empty */ }
				$sql = "";
				$sql += (((("sp_addlinkedserver @server='" . $this->getNo()) . "', @srvproduct='', @provider='SQLOLEDB', @datasrc='") . $this->getIP()) . "'");
				DBAccess::RunSQL($sql);
				$sql = "";
				$sql += ((((((" EXEC sp_addlinkedsrvlogin '" . $this->getNo()) . "','false', NULL, '") . $this->getUserID()) . "', '") . $this->getPassword()) . "'");
				DBAccess::RunSQL($sql);
				return (("恭喜您，�?(" . $this->getName()) . ")连接配置成功�?");
			}
			catch (\RuntimeException $ex)
			{
				return $ex->getMessage();
			}
		}
		if (($this->getDBSrcType() == DBSrcType::$Oracle))
		{
			try 
			{
				return (("恭喜您，�?(" . $this->getName()) . ")连接配置成功�?");
			}
			catch (\RuntimeException $ex)
			{
				return $ex->getMessage();
			}
		}
		if (($this->getDBSrcType() == DBSrcType::$MySQL))
		{
			try 
			{
				return (("恭喜您，�?(" . $this->getName()) . ")连接配置成功�?");
			}
			catch (\RuntimeException $ex)
			{
				return $ex->getMessage();
			}
		}
		return "没有涉及到的连接测试类型...";
	}
	public function GetAllTablesWithoutViews () 
	{
		$sql = new StringBuilder();
		$dbType = $this->getDBSrcType();
		if (($dbType == DBSrcType::$Localhost))
		{
			switch (SystemConfig::getAppCenterDBType()) {
				case $MSSQL:
					$dbType = DBSrcType::$SQLServer;
					break;
				case $Oracle:
					$dbType = DBSrcType::$Oracle;
					break;
				case $MySQL:
					$dbType = DBSrcType::$MySQL;
					break;
				case $Informix:
					$dbType = DBSrcType::$Informix;
					break;
				case $PostgreSQL:
					$dbType = DBSrcType::$PostgreSQL;
					break;
				default:
					throw new \RuntimeException("没有涉及到的连接测试类型...");
			}
		}
		switch ($dbType) {
			case $SQLServer:
				$sql->append(("SELECT NAME AS No," . "\r\n"));
				$sql->append(("       NAME" . "\r\n"));
				$sql->append(("FROM   sysobjects" . "\r\n"));
				$sql->append(("WHERE  xtype = 'U'" . "\r\n"));
				$sql->append(("ORDER BY" . "\r\n"));
				$sql->append(("       Name" . "\r\n"));
				break;
			case $Oracle:
				$sql->append(("SELECT uo.OBJECT_NAME No," . "\r\n"));
				$sql->append(("       uo.OBJECT_NAME Name" . "\r\n"));
				$sql->append(("  FROM user_objects uo" . "\r\n"));
				$sql->append((" WHERE uo.OBJECT_TYPE = 'TABLE'" . "\r\n"));
				$sql->append((" ORDER BY uo.OBJECT_NAME" . "\r\n"));
				break;
			case $MySQL:
				$sql->append(("SELECT " . "\r\n"));
				$sql->append(("    table_name No," . "\r\n"));
				$sql->append(("    table_name Name" . "\r\n"));
				$sql->append(("FROM" . "\r\n"));
				$sql->append(("    information_schema.tables" . "\r\n"));
				$sql->append(("WHERE" . "\r\n"));
				$sql->append(($String->format("    table_schema = '%1$s'", ( (($this->getDBSrcType() == DBSrcType::$Localhost)) ? SystemConfig::getAppCenterDBDatabase() : $this->getDBName() )) . "\r\n"));
				$sql->append(("        AND table_type = 'BASE TABLE'" . "\r\n"));
				$sql->append(("ORDER BY table_name;" . "\r\n"));
				break;
			case $Informix:
				$sql->append(("" . "\r\n"));
				break;
			case $PostgreSQL:
				$sql->append(("SELECT " . "\r\n"));
				$sql->append(("    table_name No," . "\r\n"));
				$sql->append(("    table_name Name" . "\r\n"));
				$sql->append(("FROM" . "\r\n"));
				$sql->append(("    information_schema.tables" . "\r\n"));
				$sql->append(("WHERE" . "\r\n"));
				$sql->append(($String->format("    table_schema = '%1$s'", ( (($this->getDBSrcType() == DBSrcType::$Localhost)) ? SystemConfig::getAppCenterDBDatabase() : $this->getDBName() )) . "\r\n"));
				$sql->append(("        AND table_type = 'BASE TABLE'" . "\r\n"));
				$sql->append(("ORDER BY table_name;" . "\r\n"));
				break;
			default:
				break;
		}
		$allTables = NULL;
		if ($this->getNo()->equals("local"))
		{
			$allTables = DBAccess->RunSQLReturnTable($sql);
		}
		else
		{
			try 
			{
				$allTables = DBAccess->RunSQLReturnTable($sql);
			}
			catch (\RuntimeException $ex)
			{
				throw new \RuntimeException(("@失败:" . $ex->getMessage()));
			}
		}
		return $allTables;
	}
	public function GetTables () 
	{
			/* match: b */
		return $this->GetTables_b( FALSE );
	}
	public function GetTables_b ($isCutFlowTables) // [boolean isCutFlowTables]
	{
		$sql = new StringBuilder();
		$sql->append($String->format("SELECT ss.SrcTable FROM Sys_SFTable ss WHERE ss.FK_SFDBSrc = '%1$s'", $this->getNo()));
		$allTablesExist = DBAccess->RunSQLReturnTable($sql);
		$sql->setLength(0);
		$dbType = $this->getDBSrcType();
		if (($dbType == DBSrcType::$Localhost))
		{
			switch (SystemConfig::getAppCenterDBType()) {
				case $MSSQL:
					$dbType = DBSrcType::$SQLServer;
					break;
				case $Oracle:
					$dbType = DBSrcType::$Oracle;
					break;
				case $MySQL:
					$dbType = DBSrcType::$MySQL;
					break;
				case $Informix:
					$dbType = DBSrcType::$Informix;
					break;
				case $PostgreSQL:
					$dbType = DBSrcType::$PostgreSQL;
					break;
				default:
					throw new \RuntimeException("没有涉及到的连接测试类型...");
			}
		}
		switch ($dbType) {
			case $SQLServer:
				$sql->append(("SELECT NAME AS No," . "\r\n"));
				$sql->append(("       [Name] = '[' + (CASE xtype WHEN 'U' THEN '�?' ELSE '视图' END) + '] ' + " . "\r\n"));
				$sql->append(("       NAME," . "\r\n"));
				$sql->append(("       xtype" . "\r\n"));
				$sql->append(("FROM   sysobjects" . "\r\n"));
				$sql->append(("WHERE  (xtype = 'U' OR xtype = 'V')" . "\r\n"));
				$sql->append(("       AND (NAME NOT LIKE 'Demo_%')" . "\r\n"));
				$sql->append(("       AND (NAME NOT LIKE 'Sys_%')" . "\r\n"));
				$sql->append(("       AND (NAME NOT LIKE 'WF_%')" . "\r\n"));
				$sql->append(("       AND (NAME NOT LIKE 'GPM_%')" . "\r\n"));
				$sql->append(("ORDER BY" . "\r\n"));
				$sql->append(("       xtype," . "\r\n"));
				$sql->append(("       NAME" . "\r\n"));
				break;
			case $Oracle:
				$sql->append(("SELECT uo.OBJECT_NAME AS No," . "\r\n"));
				$sql->append(("       '[' || (CASE uo.OBJECT_TYPE" . "\r\n"));
				$sql->append(("         WHEN 'TABLE' THEN" . "\r\n"));
				$sql->append(("          '�?'" . "\r\n"));
				$sql->append(("         ELSE" . "\r\n"));
				$sql->append(("          '视图'" . "\r\n"));
				$sql->append("       END) || '] ' || uo.OBJECT_NAME AS Name,");
				$sql->append(("       CASE uo.OBJECT_TYPE" . "\r\n"));
				$sql->append(("         WHEN 'TABLE' THEN" . "\r\n"));
				$sql->append(("          'U'" . "\r\n"));
				$sql->append(("         ELSE" . "\r\n"));
				$sql->append(("          'V'" . "\r\n"));
				$sql->append(("       END AS xtype" . "\r\n"));
				$sql->append(("  FROM user_objects uo" . "\r\n"));
				$sql->append((" WHERE (uo.OBJECT_TYPE = 'TABLE' OR uo.OBJECT_TYPE = 'VIEW')" . "\r\n"));
				$sql->append(("   AND uo.OBJECT_NAME NOT LIKE 'Demo_%'" . "\r\n"));
				$sql->append(("   AND uo.OBJECT_NAME NOT LIKE 'Sys_%'" . "\r\n"));
				$sql->append(("   AND uo.OBJECT_NAME NOT LIKE 'WF_%'" . "\r\n"));
				$sql->append(("   AND uo.OBJECT_NAME NOT LIKE 'GPM_%'" . "\r\n"));
				$sql->append((" ORDER BY uo.OBJECT_TYPE, uo.OBJECT_NAME" . "\r\n"));
				break;
			case $MySQL:
				$sql->append(("SELECT " . "\r\n"));
				$sql->append(("    table_name AS No," . "\r\n"));
				$sql->append(("    CONCAT('['," . "\r\n"));
				$sql->append(("            CASE table_type" . "\r\n"));
				$sql->append(("                WHEN 'BASE TABLE' THEN '�?'" . "\r\n"));
				$sql->append(("                ELSE '视图'" . "\r\n"));
				$sql->append(("            END," . "\r\n"));
				$sql->append(("            '] '," . "\r\n"));
				$sql->append("            table_name) AS Name,");
				$sql->append(("    CASE table_type" . "\r\n"));
				$sql->append(("        WHEN 'BASE TABLE' THEN 'U'" . "\r\n"));
				$sql->append(("        ELSE 'V'" . "\r\n"));
				$sql->append(("    END AS xtype" . "\r\n"));
				$sql->append(("FROM" . "\r\n"));
				$sql->append(("    information_schema.tables" . "\r\n"));
				$sql->append(("WHERE" . "\r\n"));
				$sql->append(($String->format("    table_schema = '%1$s'", ( (($this->getDBSrcType() == DBSrcType::$Localhost)) ? SystemConfig::getAppCenterDBDatabase() : $this->getDBName() )) . "\r\n"));
				$sql->append(("        AND (table_type = 'BASE TABLE'" . "\r\n"));
				$sql->append("        OR table_type = 'VIEW')");
				$sql->append(("        AND table_name NOT LIKE 'Demo_%'" . "\r\n"));
				$sql->append(("        AND table_name NOT LIKE 'Sys_%'" . "\r\n"));
				$sql->append(("        AND table_name NOT LIKE 'WF_%'" . "\r\n"));
				$sql->append(("        AND table_name NOT LIKE 'GPM_%'" . "\r\n"));
				$sql->append(("ORDER BY table_type , table_name;" . "\r\n"));
				break;
			case $Informix:
				$sql->append(("" . "\r\n"));
				break;
			case $PostgreSQL:
				$sql->append(("SELECT " . "\r\n"));
				$sql->append(("    table_name AS No," . "\r\n"));
				$sql->append(("    CONCAT('['," . "\r\n"));
				$sql->append(("            CASE table_type" . "\r\n"));
				$sql->append(("                WHEN 'BASE TABLE' THEN '�?'" . "\r\n"));
				$sql->append(("                ELSE '视图'" . "\r\n"));
				$sql->append(("            END," . "\r\n"));
				$sql->append(("            '] '," . "\r\n"));
				$sql->append("            table_name) AS Name,");
				$sql->append(("    CASE table_type" . "\r\n"));
				$sql->append(("        WHEN 'BASE TABLE' THEN 'U'" . "\r\n"));
				$sql->append(("        ELSE 'V'" . "\r\n"));
				$sql->append(("    END AS xtype" . "\r\n"));
				$sql->append(("FROM" . "\r\n"));
				$sql->append(("    information_schema.tables" . "\r\n"));
				$sql->append(("WHERE" . "\r\n"));
				$sql->append(($String->format("    table_schema = '%1$s'", ( (($this->getDBSrcType() == DBSrcType::$Localhost)) ? SystemConfig::getAppCenterDBDatabase() : $this->getDBName() )) . "\r\n"));
				$sql->append(("        AND (table_type = 'BASE TABLE'" . "\r\n"));
				$sql->append("        OR table_type = 'VIEW')");
				$sql->append(("        AND table_name NOT LIKE 'Demo_%'" . "\r\n"));
				$sql->append(("        AND table_name NOT LIKE 'Sys_%'" . "\r\n"));
				$sql->append(("        AND table_name NOT LIKE 'WF_%'" . "\r\n"));
				$sql->append(("        AND table_name NOT LIKE 'GPM_%'" . "\r\n"));
				$sql->append(("ORDER BY table_type , table_name;" . "\r\n"));
				break;
			default:
				break;
		}
		$allTables = NULL;
		if ($this->getNo()->equals("local"))
		{
			$allTables = DBAccess->RunSQLReturnTable($sql);
			$mapDT = "SELECT PTable,Name FROM Sys_MapData ";
			$myDT = DBAccess->RunSQLReturnTable($mapDT);
			foreach ($allTables->Rows as $myDR) 			{
				$no = $myDR->get("No");
				$name = NULL;
				foreach ($myDT->Rows as $dr) 				{
					$pTable = $dr->get("PTable");
					if (($pTable->equals($no) ==  FALSE ))
					{
						continue;
					}
					$name = $dr->get("Name");
					break;
				}
				if (($name . NULL))
				{
					$myDR->setValue("Name", (($myDR->get("Name") . "-") . $name));
				}
			}
		}
		else
		{
			try 
			{
				$allTables = DBAccess->RunSQLReturnTable($sql);
			}
			catch (\RuntimeException $ex)
			{
				throw new \RuntimeException(("@失败:" . $ex->getMessage()));
			}
		}
		$filter = "";
		foreach ($allTablesExist->Rows as $dr) 		{
			$filter += $String->format("No='%1$s' OR ", $dr->getValue(0));
		}
		if (!$filter->equals(""))
		{
			$deletedRows = $allTables->select(SFDBSrc::rtrim($filter, " OR "));
			foreach ($deletedRows as $dr) 			{
				$allTables->Rows->remove($dr);
			}
		}
		if (($isCutFlowTables ==  TRUE ))
		{
			$dt = new DataTable();
			$dt->Columns->Add("No", String);
			$dt->Columns->Add("Name", String);
			foreach ($allTables->Rows as $dr) 			{
				$no = $dr->get("No");
				if (((($no->contains("WF_") || $no->contains("Track")) || $no->contains("Sys_")) || $no->contains("Demo_")))
				{
					continue;
				}
				$mydr = $dt->NewRow();
				$mydr->setValue("No", $dr->get("No"));
				$mydr->setValue("Name", $dr->get("Name"));
				$dt->Rows->add($mydr);
			}
			return $dt;
		}
		return $allTables;
	}
	public static function rtrim ($str, $substr) // [String str, String substr]
	{
		$j = ($str->length() - 1);
		for (; ($j > -1); --$j) 
		{
			if (($substr->indexOf($str->charAt($j)) == -1))
			{
				break;
			}
		}
		return $str->substring(0, ($j + 1));
	}
	protected function GetDSN () 
	{
		$dsn = "";
		$dbType = $this->getDBSrcType();
		if (($dbType == DBSrcType::$Localhost))
		{
			switch (SystemConfig::getAppCenterDBType()) {
				case $MSSQL:
					$dbType = DBSrcType::$SQLServer;
					break;
				case $Oracle:
					$dbType = DBSrcType::$Oracle;
					break;
				case $MySQL:
					$dbType = DBSrcType::$MySQL;
					break;
				case $Informix:
					$dbType = DBSrcType::$Informix;
					break;
				case $PostgreSQL:
					$dbType = DBSrcType::$PostgreSQL;
					break;
				default:
					throw new \RuntimeException("没有涉及到的连接测试类型...");
			}
		}
		switch ($dbType) {
			case $SQLServer:
				$dsn = (((((((("Password=" . $this->getPassword()) . ";Persist Security Info=True;User ID=") . $this->getUserID()) . ";Initial Catalog=") . $this->getDBName()) . ";Data Source=") . $this->getIP()) . ";Timeout=999;MultipleActiveResultSets=true");
				break;
			case $Oracle:
				$dsn = (((((("user id=" . $this->getUserID()) . ";data source=") . $this->getDBName()) . ";password=") . $this->getPassword()) . ";Max Pool Size=200");
				break;
			case $MySQL:
				$dsn = (((((((("Data Source=" . $this->getIP()) . ";Persist Security info=True;Initial Catalog=") . $this->getDBName()) . ";User ID=") . $this->getUserID()) . ";Password=") . $this->getPassword()) . ";");
				break;
			case $Informix:
				$dsn = (((((((("Provider=Ifxoledbc;Data Source=" . $this->getDBName()) . "@") . $this->getIP()) . ";User ID=") . $this->getUserID()) . ";Password=") . $this->getPassword()) . ";");
				break;
			default:
				throw new \RuntimeException("没有涉及到的连接测试类型...");
		}
		return $dsn;
	}
	public function Rename_String_String_String ($objType, $oldName, $newName) // [String objType, String oldName, String newName]
	{
			/* match: String_String_String_String */
		$this->Rename_String_String_String_String($objType, $oldName, $newName, NULL);
	}
	public function Rename_String_String_String_String ($objType, $oldName, $newName, $tableName) // [String objType, String oldName, String newName, String tableName]
	{
		$dbType = $this->getDBSrcType();
		if (($dbType == DBSrcType::$Localhost))
		{
			switch (SystemConfig::getAppCenterDBType()) {
				case $MSSQL:
					$dbType = DBSrcType::$SQLServer;
					break;
				case $Oracle:
					$dbType = DBSrcType::$Oracle;
					break;
				case $MySQL:
					$dbType = DBSrcType::$MySQL;
					break;
				case $Informix:
					$dbType = DBSrcType::$Informix;
					break;
				default:
					throw new \RuntimeException("@没有涉及到的连接测试类型�?");
			}
		}
		switch ($dbType) {
			case $SQLServer:
				if ($objType->toLowerCase()->equals("column"))
				{
					$this->RunSQL($String->format("EXEC SP_RENAME '%1$s', '%2$s', 'COLUMN'", $oldName, $newName));
				}
				else
				{
					$this->RunSQL($String->format("EXEC SP_RENAME '%1$s', '%2$s'", $oldName, $newName));
				}
				break;
			case $Oracle:
				if ($objType->toLowerCase()->equals("column"))
				{
					$this->RunSQL($String->format("ALTER TABLE %1$s RENAME COLUMN %2$s TO %3$s", $tableName, $oldName, $newName));
				}
				else
					if ($objType->toLowerCase()->equals("table"))
					{
						$this->RunSQL($String->format("ALTER TABLE %1$s RENAME TO %2$s", $oldName, $newName));
					}
					else
						if ($objType->toLowerCase()->equals("view"))
						{
							$this->RunSQL($String->format("RENAME %1$s TO %2$s", $oldName, $newName));
						}
						else
						{
							throw new \RuntimeException("@未涉及到的Oracle数据库改名�?�辑�?");
						}
				break;
			case $MySQL:
				if ($objType->toLowerCase()->equals("column"))
				{
					$sql = $String->format("SELECT c.COLUMN_TYPE FROM information_schema.columns c WHERE c.TABLE_SCHEMA = '%1$s' AND c.TABLE_NAME = '%2$s' AND c.COLUMN_NAME = '%3$s'", $this->getDBName(), $tableName, $oldName);
					$dt = $this->RunSQLReturnTable($sql);
					if (($dt->Rows->size() > 0))
					{
						$this->RunSQL($String->format("ALTER TABLE %1$s CHANGE COLUMN %2$s %3$s %4$s", $tableName, $oldName, $newName, $dt->Rows->get(0)->getValue(0)));
					}
				}
				else
					if ($objType->toLowerCase()->equals("table"))
					{
						$this->RunSQL($String->format("ALTER TABLE `%1$s`.`%2$s` RENAME `%1$s`.`%3$s`", $this->getDBName(), $oldName, $newName));
					}
					else
						if ($objType->toLowerCase()->equals("view"))
						{
							$sql = $String->format("SELECT t.VIEW_DEFINITION FROM information_schema.views t WHERE t.TABLE_SCHEMA = '%1$s' AND t.TABLE_NAME = '%2$s'", $this->getDBName(), $oldName);
							$dt = $this->RunSQLReturnTable($sql);
							if (($dt->Rows->size() == 0))
							{
								$this->RunSQL(("DROP VIEW " . $oldName));
							}
							else
							{
								$this->RunSQL($String->format("CREATE VIEW %1$s AS %2$s", $newName, $dt->Rows->get(0)->getValue(0)));
								$this->RunSQL(("DROP VIEW " . $oldName));
							}
						}
						else
						{
							throw new \RuntimeException("@未涉及到的Oracle数据库改名�?�辑�?");
						}
				break;
			case $Informix:
				break;
			default:
				throw new \RuntimeException("@没有涉及到的数据库类型�??");
		}
	}
	public function GetColumns ($tableName) // [String tableName]
	{
		$sql = new StringBuilder();
		$dbType = $this->getDBSrcType();
		if (($dbType == DBSrcType::$Localhost))
		{
			switch (SystemConfig::getAppCenterDBType()) {
				case $MSSQL:
					$dbType = DBSrcType::$SQLServer;
					break;
				case $Oracle:
					$dbType = DBSrcType::$Oracle;
					break;
				case $MySQL:
					$dbType = DBSrcType::$MySQL;
					break;
				case $Informix:
					$dbType = DBSrcType::$Informix;
					break;
				default:
					throw new \RuntimeException("没有涉及到的连接测试类型...");
			}
		}
		switch ($dbType) {
			case $SQLServer:
				$sql->append(("SELECT sc.name as No," . "\r\n"));
				$sql->append(("       st.name AS [DBType]," . "\r\n"));
				$sql->append(("       (" . "\r\n"));
				$sql->append(("           CASE " . "\r\n"));
				$sql->append(("                WHEN st.name = 'nchar' OR st.name = 'nvarchar' THEN sc.length / 2" . "\r\n"));
				$sql->append(("                ELSE sc.length" . "\r\n"));
				$sql->append(("           END" . "\r\n"));
				$sql->append("       ) AS DBLength,");
				$sql->append(("       sc.colid," . "\r\n"));
				$sql->append(($String->format("       %1$s AS [Name]", $SqlBuilder->GetIsNullInSQL("ep.[value]", "''")) . "\r\n"));
				$sql->append(("FROM   dbo.syscolumns sc" . "\r\n"));
				$sql->append(("       INNER JOIN dbo.systypes st" . "\r\n"));
				$sql->append(("            ON  sc.xtype = st.xusertype" . "\r\n"));
				$sql->append(("       LEFT OUTER JOIN sys.extended_properties ep" . "\r\n"));
				$sql->append(("            ON  sc.id = ep.major_id" . "\r\n"));
				$sql->append(("            AND sc.colid = ep.minor_id" . "\r\n"));
				$sql->append(("            AND ep.name = 'MS_Description'" . "\r\n"));
				$sql->append(($String->format("WHERE  sc.id = OBJECT_ID('dbo.%1$s')", $tableName) . "\r\n"));
				break;
			case $Oracle:
				$sql->append(("SELECT utc.COLUMN_NAME AS No," . "\r\n"));
				$sql->append(("       utc.DATA_TYPE   AS DBType," . "\r\n"));
				$sql->append(("       utc.CHAR_LENGTH AS DBLength," . "\r\n"));
				$sql->append(("       utc.COLUMN_ID   AS colid," . "\r\n"));
				$sql->append(($String->format("       %1$s    AS Name", $SqlBuilder->GetIsNullInSQL("ucc.comments", "''")) . "\r\n"));
				$sql->append(("  FROM user_tab_cols utc" . "\r\n"));
				$sql->append(("  LEFT JOIN user_col_comments ucc" . "\r\n"));
				$sql->append(("    ON ucc.table_name = utc.TABLE_NAME" . "\r\n"));
				$sql->append(("   AND ucc.column_name = utc.COLUMN_NAME" . "\r\n"));
				$sql->append(($String->format(" WHERE utc.TABLE_NAME = '%1$s'", $tableName->toUpperCase()) . "\r\n"));
				$sql->append((" ORDER BY colid ASC" . "\r\n"));
				break;
			case $MySQL:
				$sql->append(("SELECT " . "\r\n"));
				$sql->append(("    column_name AS 'No'," . "\r\n"));
				$sql->append(("    data_type AS 'DBType'," . "\r\n"));
				$sql->append(($String->format("    %1$s AS DBLength,", $SqlBuilder->GetIsNullInSQL("character_maximum_length", "numeric_precision")) . "\r\n"));
				$sql->append(("    ordinal_position AS colid," . "\r\n"));
				$sql->append(("    column_comment AS 'Name'" . "\r\n"));
				$sql->append(("FROM" . "\r\n"));
				$sql->append(("    information_schema.columns" . "\r\n"));
				$sql->append(("WHERE" . "\r\n"));
				$sql->append(($String->format("    table_schema = '%1$s'", ( (($this->getDBSrcType() == DBSrcType::$Localhost)) ? SystemConfig::getAppCenterDBDatabase() : $this->getDBName() )) . "\r\n"));
				$sql->append(($String->format("        AND table_name = '%1$s';", $tableName) . "\r\n"));
				break;
			case $Informix:
				break;
			default:
				throw new \RuntimeException("没有涉及到的连接测试类型...");
		}
		$dt = NULL;
		if (($this->getNo()->equals("local") ==  TRUE ))
		{
			$dt = DBAccess->RunSQLReturnTable($sql);
			return $dt;
		}
		return NULL;
	}
	protected function beforeDelete () 
	{
		if ($this->getNo()->equals("local"))
		{
			throw new \RuntimeException("@默认连接(local)不允许删除�?�更�?.");
		}
		$str = "";
		$mds = MapDatas::constructor__();
		$mds->Retrieve(MapDataAttr::$DBSrc, $this->getNo());
		if (($mds->size() != 0))
		{
			$str += "如下表单使用了该数据源，您不能删除它�?";
			foreach ($mds->ToJavaList() as $md) 			{
				$str += ((("@\t\n" . $md->getNo()) . " - ") . $md->getName());
			}
		}
		$tabs = SFTables::constructor__();
		$tabs->Retrieve(SFTableAttr::$FK_SFDBSrc, $this->getNo());
		if (($tabs->size() != 0))
		{
			$str += "如下 table 使用了该数据源，您不能删除它�?";
			foreach ($tabs->ToJavaList() as $tab) 			{
				$str += ((("@\t\n" . $tab->getNo()) . " - ") . $tab->getName());
			}
		}
		if (!$str->equals(""))
		{
			throw new \RuntimeException(("@删除数据源的时�?�检查，是否有引用，出现错误�?" . $str));
		}
		return parent->beforeDelete();
	}
	protected function beforeUpdate () 
	{
		if ($this->getNo()->equals("local"))
		{
			throw new \RuntimeException("@默认连接(local)不允许删除�?�更�?.");
		}
		return parent->beforeUpdate();
	}
	protected function beforeUpdateInsertAction () 
	{
		if ((!$this->getNo()->equals("local") && ($this->getDBSrcType() == DBSrcType::$Localhost)))
		{
			throw new \RuntimeException("@在该系统中只能有�?个本地连接，请�?�择其他数据源类型�??");
		}
		return parent->beforeUpdateInsertAction();
	}
}
?>
